package com.party.service.info.impl;

import com.party.common.Page;
import com.party.mapper.UserInfoMapper;
import com.party.model.UserInfo;
import com.party.model.UserInfoExample;
import com.party.service.info.InfoService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class InfoServiceImpl implements InfoService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public int addUserInfo(UserInfo userInfo) {
        int num = userInfoMapper.insertSelective(userInfo);
        return num;
    }

    @Override
    public int deleteUserInfoByPrimaryKey(Integer id) {
        int num = userInfoMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += userInfoMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public UserInfo getUserInfoByPrimaryKey(Integer id) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        return userInfo;
    }

    @Override
    public int updateUserInfoByPrimaryKey(UserInfo userInfo) {
        int num = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        return num;
    }

    @Override
    public Page getUserInfoList(UserInfo userInfo, int pageSize, int pageNumber) {
        UserInfoExample userInfoExample = new UserInfoExample();
        /**
         * 可以添加查询条件
         */
        if(userInfo != null)
        {
            UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
            if(userInfo.getTitle() != null && !"".equals(userInfo.getTitle()))
            {
                criteria.andTitleLike("%"+userInfo.getTitle()+"%");
            }

            if(userInfo.getUid() != null && !"".equals(userInfo.getUid()))
            {
                criteria.andUidEqualTo(userInfo.getUid());
            }

            if(userInfo.getTime() != null && !"".equals(userInfo.getTime()))
            {
                criteria.andTimeGreaterThan(userInfo.getTime());
            }
        }
        userInfoExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = userInfoMapper.countByExample(userInfoExample);
        //获取指定的页码数据
        userInfoExample.setBeginNum((pageNumber - 1) * pageSize);
        userInfoExample.setEndNum(pageSize);
        List<UserInfo> list = userInfoMapper.getListPage(userInfoExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
